﻿namespace BEGDC.CC.Web.UserControls.ESC
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Collections;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using BEGDC.CC.BL.CCFlow.ESC;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.Utility.Castle;
	using BEGDC.CC.BL.Common.Query;

	/// <summary>
	///		Summary description for CheckApp.
	/// </summary>
	public class CheckApp : BaseESC
	{
		#region Variables
		protected BEGDC.CC.Web.UserControls.ESC.ESCCtrl ESCCtrl;
		protected System.Web.UI.WebControls.Button Btn_Submit;
		protected System.Web.UI.WebControls.Button Btn_Query;
		protected System.Web.UI.WebControls.TextBox tbOperationFlag;
		protected System.Web.UI.WebControls.TextBox tbOperationContent;
		protected System.Web.UI.WebControls.Button Btn_Reject;
		protected System.Web.UI.WebControls.Button Btn_Save;
		protected System.Web.UI.WebControls.TextBox tbComment;
		protected System.Web.UI.WebControls.Button Btn_Reply;
		protected BEGDC.CC.Web.Common.StampCtrl StampCtrl1;
		protected System.Web.UI.WebControls.Label lbAppNo;
		protected BEGDC.CC.Web.UserControls.CV.CVPrintCtrl cvPrintCtrl;
        protected System.Web.UI.WebControls.Button btnClose;
		bool isReadOnly = false;
		protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenScrollHight;
		bool saveBeforeSubmit=false;//joan 2008/8/23
		#endregion

		#region Properties
		private bool IsQuery
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				return state == SysDicItem.ProjectStatusFlag.QUERY;
			}
		}
		private bool IsView
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				if(state == SysDicItem.ProjectStatusFlag.View || state == SysDicItem.ProjectStatusFlag.CANCEL
					|| state == SysDicItem.ProjectStatusFlag.Pending || state == SysDicItem.ProjectStatusFlag.End
					|| state == SysDicItem.ProjectStatusFlag.Participation)
					return true;
				else
					return false;
			}
		}
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				base.PreInitForm();

				this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
				this.Btn_Query.Attributes.Add("onclick", "javascript:OpenQueryPane("+Request.QueryString["AppID"]+");");
				this.Btn_Reject.Attributes.Add("onclick", "javascript:OpenCommentPane();");
				this.Btn_Reply.Attributes.Add("onclick", "javascript:OpenReplyPane("+Request.QueryString["FlowID"]+");");
                this.btnClose.Attributes.Add("onclick", "javascript:SimpleCloseWindow();");

				InitForm();
			}
			ButtonStatusSetting();
		}

		#region ControlStatusSetting
		private void ControlStatusSetting(Accessibility access )
		{
			if( access == Accessibility.Readonly )
			{
				this.DisableChildrenControls(this);
				//this.stampCtrl1.Visible = false;
			}
		}
		#endregion

		#region InitForm
		private void InitForm()
		{
			long flowId  = this.LoadApplication();

			if(this.Application!=null)
			{
				if(this.Application.AppNo != string.Empty)
					this.lbAppNo.Text = "( "+this.Application.AppNo+" )";

				ESCCtrl.isCoveredPerson = !this.Application.IsTransition;
				ESCCtrl.isReadOnly = isReadOnly;
				ESCCtrl.DisableEditESC();
			}
			else
			{
				WebUtility.Go2ErrorPage( this.Page, "wrong Application ID" );
			}
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Btn_Reply.Click += new EventHandler(this.btnReply_Click);
			this.Btn_Query.Click += new EventHandler(this.Btn_Query_Click);
			this.Btn_Reject.Click += new EventHandler(this.Btn_Reject_Click);
			this.Btn_Save.Click += new EventHandler(this.Btn_Save_Click);
			this.Btn_Submit.Click += new EventHandler(this.Btn_Submit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region BuildObjects
		private DataTable BuildSessionTable(string SessionName)
		{
			DataTable dataTable = null;
			object SessDataTable = Session[SessionName];
			if(SessDataTable!=null)
			{
				dataTable = (DataTable)SessDataTable;
			}
			return dataTable;
		}
		#endregion

		#region Save
		private void Btn_Save_Click(object sender, EventArgs e)
		{
			string url = string.Empty + Request.Form["tbURL"];
			string script = "<script>";
			try
			{
				ImplementSave();

				if(url == string.Empty)
					script += "VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');";
				if(url != string.Empty)
				{
					script += "window.location.href='"+url+"'";
				}
				script += "</script>";

				
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
			}
			if (!this.saveBeforeSubmit)
			{
				Page.RegisterClientScriptBlock("",script);
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');</script>");
			}
		}

		private void ImplementSave()
		{
			
			try
			{
				//OBTAIN BASIC INFORMATION
				IESC ESCService = AppContainer.Instance[ServiceConst.ESCService] as IESC;
				BEGDC.CC.Entity.Application App = ESCService.GetApplication(this.AppID);

				Application app = new Application();
				if(this.Application!=null)
				{
					app.Id = this.AppID;
					app.AppNo = this.Application.AppNo;
					app.IsTransition = this.Application.IsTransition;
					app.NeedEVPApproval = this.Application.NeedEVPApproval;
					app.DepartmentType = this.Application.DepartmentType;
					app.DepartmentName = this.Application.DepartmentName;
				}

				CustomerInfo customer = new CustomerInfo();
				BusinessPurpose business = new BusinessPurpose();
				Topic topic = new Topic();
				SalesTeamInfo salesTeam = new SalesTeamInfo();
				LogisticalInfo logist = new LogisticalInfo();
				PaymentInfo payment = new PaymentInfo();
				IList customerAttendee = new ArrayList();
				IList alAttendee = new ArrayList();
				IList site = new ArrayList();
				IList cost = new ArrayList();
				IList contract = new ArrayList();
				//update Jovi
				DataTable HotelTable = null;
				DataTable CompanyCarTable = null;
				DataTable LeaseCarTable = null;

				ESCCtrl.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract,ref HotelTable, ref CompanyCarTable, ref LeaseCarTable);
				ESCService.SaveEZVisitCheckApplication(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost, contract,HotelTable, CompanyCarTable, LeaseCarTable,false, true);

				this.AppID = app.Id;
				this.Application = app;
				ESCCtrl.CustomerID = customer.Id;
				ESCCtrl.TopicID = topic.Id;
				ESCCtrl.SalesTeamID = salesTeam.Id;
				ESCCtrl.LogistID = logist.Id;
				ESCCtrl.BusinessID = business.Id;
				ESCCtrl.PaymentID = payment.Id;
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
			}
		}
		#endregion

		#region Submit
        private void Btn_Submit_Click(object sender, EventArgs e)
		{
			try
			{
				//<--joan 2008/8/23
				string msg;
				this.saveBeforeSubmit=true;
				this.Btn_Save_Click(this.Btn_Save,null);
				this.saveBeforeSubmit=false;
				msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage( this.AppID );
				//-->end
				if( msg!="" && msg!=string.Empty)
				{
					Page.RegisterClientScriptBlock("","<script>alert('" + msg + "')</script>");
					return;
				}

				//OBTAIN BASIC INFORMATION
				IESC ESCService = AppContainer.Instance[ServiceConst.ESCService] as IESC;

				//DECISION QUERY/SUBMIT
				if(this.tbOperationFlag.Text == "reply")
				{
					//CRService.SaveCheckApplication(App,customerinfo,logisticinfo,VisitingList, CustomerTable,AccompanyTable,HotelTable,CompanyCarTable,LeaseCarTable);
					ESCService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]),WebUtility.DeCodeString(Server, tbComment.Text));
				}
				else
				{
					BEGDC.CC.Entity.Application App = ESCService.GetApplication(this.AppID);

					Application app = new Application();
					if(this.Application!=null)
					{
						app.Id = this.AppID;
						app.AppNo = this.Application.AppNo;
						app.IsTransition = this.Application.IsTransition;
						app.NeedEVPApproval = this.Application.NeedEVPApproval;
						app.DepartmentType = this.Application.DepartmentType;
						app.DepartmentName = this.Application.DepartmentName;
					}

					CustomerInfo customer = new CustomerInfo();
					BusinessPurpose business = new BusinessPurpose();
					Topic topic = new Topic();
					SalesTeamInfo salesTeam = new SalesTeamInfo();
					LogisticalInfo logist = new LogisticalInfo();
					PaymentInfo payment = new PaymentInfo();
					IList customerAttendee = new ArrayList();
					IList alAttendee = new ArrayList();
					IList site = new ArrayList();
					IList cost = new ArrayList();
					IList contract = new ArrayList();
					//update Jovi
					DataTable HotelTable = null;
					DataTable CompanyCarTable = null;
					DataTable LeaseCarTable = null;

					ESCCtrl.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract,ref HotelTable, ref CompanyCarTable, ref LeaseCarTable);
					ESCService.SubmitEZVisitCheckApplication(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost,contract,HotelTable, CompanyCarTable, LeaseCarTable,true,this.tbComment.Text);
				}

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				if (exp.InnerException !=null)
				
					Page.RegisterClientScriptBlock("","<script>alert('" + exp.InnerException.Message + "');</script>");
				else
					Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
			}
		}
		#endregion

		#region Query
        private void Btn_Query_Click(object sender, EventArgs e)
		{
			try
			{
				if(this.tbOperationFlag.Text == "query")
				{
					IESC ESCService = AppContainer.Instance[ServiceConst.ESCService] as IESC;
					ESCService.Query(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());

					Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
				}
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
			}
		}
		#endregion

		#region Reject
        private void Btn_Reject_Click(object sender, EventArgs e)
		{
			try
			{
				IESC ESCService = AppContainer.Instance[ServiceConst.ESCService] as IESC;
				ESCService.RejectCheckApplication(Convert.ToInt64(Request.QueryString["FlowID"]), WorkTransition.Reject, this.tbComment.Text);

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
			}
		}
		#endregion

		#region Export
        private void Btn_Export_Click(object sender, EventArgs e)
		{
			try
			{
				ExcelTool.ICCExcelExport excelExport;
				if (this.Application.IsTransition)
					excelExport=new ExcelTool.ExportCRFormA();
				else
					excelExport=new ExcelTool.ExportCRFormB();
				excelExport.Export(this.Page, this.AppID );
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");

			}
			catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
			}
		}
		#endregion

		#region ButtonStatusSetting
		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.Btn_Save.Visible = this.Access.IsButtonVisible(ESCTransitionList.Save);
			this.Btn_Query.Visible = this.Access.IsButtonVisible(ESCTransitionList.Query);
			this.Btn_Submit.Visible = this.Access.IsButtonVisible(ESCTransitionList.Check);
			this.Btn_Reject.Visible = this.Access.IsButtonVisible(ESCTransitionList.Reject);
			this.Btn_Reply.Visible = this.Access.IsButtonVisible(ESCTransitionList.Reply);
			this.cvPrintCtrl.ProcessType = WorkflowTypes.ESC;
			this.cvPrintCtrl.Visible = this.Access.IsButtonVisible(ESCTransitionList.Copy);

			if((this.Access.IsReadOnly || this.IsView))
				isReadOnly = true;
		}
		#endregion

		#region Reply
        private void btnReply_Click(object sender, EventArgs e)
		{
			try
			{
				IESC ESCService = AppContainer.Instance[ServiceConst.ESCService] as IESC;

				ImplementSave();

				ESCService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]),WebUtility.DeCodeString(Server, tbComment.Text));

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
			}
		}
		#endregion
	}
}
